# Replication file for "Weathering the Storm: US Trade Policy Beyond Trump"
# Andreas Dür & Alessia Invernizzi 
# 18 September 2025

# Packages
library(ggplot2)
library(dplyr)
library(tidyverse)
library(countrycode)
library(data.table)
library(patchwork)
library(tibble)


# set path ----------------------------------------------------------------


setwd(this.path::this.dir())


# Load data 
gta_usa <- fread("gta_usa.csv") %>% 
  mutate(
    rival_ever = if_else(ccode_a %in% c("CHN", "RUS", "CUB"), "Rival", "Ally"),
    month = as.numeric(month),   
    period = paste0(
      year, "-", 
      ifelse(month <= 4, "1",
             ifelse(month <= 8, "2", "3"))
    )
  ) %>% 
  arrange(period) %>%
  rename(intervention_id = Intervention.ID)


# FIGURE 1
gta_usa %>%
filter(evaluation=="Red") %>%
  group_by(period, measure) %>%
  summarise(Count = n_distinct(intervention_id[intervention_id!=0]), .groups = "drop") %>%
  mutate(period = factor(period, levels = sort(unique(period)))) %>%
  ggplot(aes(x = period, y = Count, fill = measure)) +
  geom_col(position = position_dodge(preserve = "single"),
           width = .8) +  
  theme_minimal() +
  geom_vline( 
    xintercept = which(levels(factor(sort(unique(gta_usa$period)))) == "2022-3") - 0.5,
    linetype = "dashed", color = "black"
  )+
  labs(x = "Periods", 
       y = "Count of interventions",
       fill = "Restrictions") +
  scale_fill_manual(
    values = c("Import_res" = "seagreen", "Export_res" = "darkblue"),
    labels = c("Import_res" = "Import", 
               "Export_res" = "Export")
  ) +
  scale_x_discrete(
    breaks = function(x) x[seq(1, length(x), by = 2)]
  ) +
  theme(
    legend.position = c(0.01, 0.99),
    legend.background = element_blank(),
    text = element_text(size = 10, color = "black"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )


ggsave(filename = "usa_year_measure.png",
       width = 6, height = 3)



# FIGURE 3
gta_usa %>%
  filter(evaluation=="Red") %>%
  filter(month<=8 & year >=2009) %>%
  filter(ccode_a %in% c("CHN", "RUS", "CUB", "CAN", "GBR","DEU")) %>%
  group_by(year, security_di, rival_ever, measure) %>%
  summarise(Count = n_distinct(intervention_id[intervention_id!=0]), .groups = "drop") %>%
  mutate(
    security_label = ifelse(security_di == 1, "Security", "No Security"),
    rival_label = factor(rival_ever, levels = c("Ally", "Rival")),
    measure_di = factor(measure, levels = c("Import_res", "Export_res"), labels = c("Import restriction", "Export restriction"))
  ) %>%
  ggplot(aes(
    x = year,
    y = Count,
    fill = rival_label
  )) +
  geom_col(
    position = position_dodge(preserve = "single"),
    width = .8
  ) +
  facet_grid(measure_di ~ security_label,
             labeller = labeller(
               security_label = c("Security" = "Security rationale",
                                  "No Security" = "No security rationale")
             )) +
  theme_minimal() +
  labs(
    x = "Year",
    y = "Count of interventions",
    fill = "Alignment"
  ) +
  scale_x_continuous(,seq(2009,2025))+
  scale_fill_manual(
    values = c("Ally" = "darkblue", "Rival" = "darkred")) +
  theme(
    text = element_text(size = 10, color = "black"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = c(0.00, 0.99),
    legend.justification = c("left", "top"),
    legend.background = element_blank()
  )


ggsave(filename = "usa_year_rival_security.png", 
       width = 6, height = 4)




#### APPENDIX

# FIGURE A1
gta_oecd <- fread("OECD_interventions.csv") 

gta_oecd %>% 
  filter(evaluation=="Red") %>%
  group_by(period, measure) %>%
  summarise(Count = n_distinct(intervention_id[intervention_id!=0]), .groups = "drop") %>%
  mutate(period = factor(period, levels = sort(unique(period)))) %>%
  ggplot(aes(x = period, y = Count, fill = measure)) +
  geom_col(position = position_dodge(preserve = "single"),
               width = .8) +   # side-by-side bars
  theme_minimal() +
  scale_x_discrete(
    breaks = function(x) x[seq(1, length(x), by = 2)]
  ) +
  geom_vline(
    xintercept = which(levels(factor(sort(unique(gta_oecd$period)))) == "2024-3") - 0.5,
    linetype = "dashed", color = "black"
  ) +
  labs(x = "Periods", 
       y = "Count of interventions",
       fill = "Restrictions") +
  scale_fill_manual(
    values = c("Import_res" = "seagreen", "Export_res" = "darkblue"),
    labels = c("Import_res" = "Import", 
               "Export_res" = "Export")
  ) +
  theme(
    legend.position = c(0.01, 0.99),
    legend.justification = c("left", "top"),
    legend.background = element_blank(),
    text = element_text(size = 10, color = "black"),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

ggsave(filename = "oecd_year_measure.png",
       width = 6, height = 3)



# FIGURE A2

gta_usa %>%
  filter(evaluation=="Red") %>%
  filter(month<=8 & year >=2009) %>%
  filter(evaluation=="Red") %>%
  filter(affected_country %in% c( "Cuba", "China", "Russia", "Canada", "United Kingdom", 
                                  "Mexico", "Japan", "Germany", "France", "Switzerland")) %>%
  mutate(affected_country = factor(
    affected_country,
    levels = c("Cuba", "China", "Russia", "Japan", "Canada", "Mexico", "United Kingdom", 
               "Germany", "France", "Switzerland")
  )) %>%
  mutate(affected_country= if_else(affected_country=="United Kingdom", "UK", affected_country))%>%
  group_by(year, security_di, affected_country, measure) %>%
  summarise(Count = n_distinct(intervention_id[intervention_id!=0]), .groups = "drop") %>%
  mutate(
    security_label = ifelse(security_di == 1, "Security", "No Security"),
    measure_di = factor(measure, levels = c("Import_res", "Export_res"), labels = c("Import", "Export"))
  ) %>%
  ggplot(aes(
    x = year,
    y = Count,
    fill = measure_di
  )) +
  geom_col(
    position = position_dodge(preserve = "single"),
    width = .8
  ) +
  facet_grid(affected_country ~ security_label,
             labeller = labeller(
               security_label = c("Security" = "Security rationale",
                                  "No Security" = "No security rationale")
             )) +
  theme_minimal() +
  labs(
    x = "Year",
    y = "Count of interventions",
    fill = " Restrictions"
  ) +
  scale_x_continuous(,seq(2009,2025))+
  scale_fill_manual(
    values = c("Export" = "darkblue", "Import" = "seagreen")) +
  theme(
    text = element_text(size = 10, color = "black"),
    axis.text.x = element_text(angle = 45, hjust = 1),
    legend.position = c(-0.02, 1.05),
    legend.justification = c("left", "top"),
    legend.background = element_blank()
  )

ggsave(filename = "usa_country.png", 
       width = 6, height = 8)


# Figure A3 ---------------------------------------------------------------


# ---- reusable plotting function (no panel title; panel has only a subtitle)

poll_bar <- function(df, subtitle = NULL) {
  df <- df %>% mutate(option = factor(option, levels = rev(option)))
  ggplot(df, aes(x = value, y = option)) +
    geom_col(width = 0.7) +
    geom_text(aes(label = paste0(value, "%")), x = df$value + 1, hjust = 0, size = 3.8) +
    scale_x_continuous(limits = c(0, 75),
                       breaks = seq(0, 75, 25),
                       labels = function(x) paste0(x, "%"),
                       expand = expansion(mult = c(0, 0.05))) +
    labs(x = NULL, y = NULL, subtitle = subtitle) +
    theme_minimal(base_size = 12) +
    theme(
      panel.grid.major.y = element_blank(),
      panel.grid.minor = element_blank(),
      axis.text.y = element_text(size = 11),
      plot.subtitle = element_text(face = "bold"),
      plot.margin = margin(8, 16, 8, 8)
    ) +
    coord_cartesian(clip = "off")
}

## ---- data ----

df1 <- tibble::tribble(
  ~option,          ~value,
  "Helps",            31,
  "Hurts",            45,
  "No difference",    12,
  "Don't know",       11
)

df2 <- tibble::tribble(
  ~option,                        ~value,
  "Helps US economy",               32,
  "Hurts US economy",               58,
  "Doesn't make much difference",   10
)

## ---- build panels (subtitles identify each poll) ----

p1 <- poll_bar(
  df1,
  subtitle = "Sept 15–17, 2019 (registered voters, n = 1008)"
)

p2 <- poll_bar(
  df2,
  subtitle = "May 5–15, 2025 (adults, n = 1004)"
)

## ---- combine with one common title & caption ----

common_title <- "In general, do you think imposing tariffs or fees on products imported from other\ncountries helps the United States economy, hurts the economy,\nor doesn't make much of a difference either way?"
common_caption <- paste(
  "Sources: Fox News Poll 2019 and Marquette Law School Poll 2025 via the Roper Center at Cornell University.",
  "Geographic coverage: United States • Survey orgs: Beacon Research & Shaw & Co.; SSRS • Sponsors: Fox News; Marquette Law School.",
  sep = "\n"
)

combined <- (p1 / p2) +
  plot_annotation(
    title = common_title,
    caption = common_caption,
    theme = theme(
      plot.title = element_text(face = "bold", size = 13, margin = margin(b = 6)),
      plot.caption = element_text(size = 8, color = "grey30", margin = margin(t = 8))
    )
  )

## view

combined

## save

ggsave("helps_hurt.png", combined, width = 7.5, height = 8.5, dpi = 300)


# Figure A4 ---------------------------------------------------------------


## --- horizontal percentage bar panel (panel has only a subtitle) ---


poll_panel <- function(df, subtitle) {
  df <- df %>% mutate(option = factor(option, levels = rev(option)))  # keep given order
  ggplot(df, aes(x = value, y = option)) +
    geom_col(width = 0.7) +
    geom_text(aes(label = paste0(value, "%")),
              x = df$value + 1, hjust = 0, size = 3.8) +
    scale_x_continuous(limits = c(0, 75),
                       breaks = seq(0, 75, 25),
                       labels = function(x) paste0(x, "%"),
                       expand = expansion(mult = c(0, 0.05))) +
    labs(x = NULL, y = NULL, subtitle = subtitle) +
    theme_minimal(base_size = 12) +
    theme(
      panel.grid.major.y = element_blank(),
      panel.grid.minor = element_blank(),
      axis.text.y = element_text(size = 11),
      plot.subtitle = element_text(face = "bold"),
      plot.margin = margin(8, 16, 8, 8)
    ) +
    coord_cartesian(clip = "off")
}

## --- data (CNN April 2025) ---

short_term <- tribble(
  ~option,                     ~value,
  "Help",                        12,
  "Hurt",                        72,
  "Neither help nor hurt",       16
)

long_term <- tribble(
  ~option,                     ~value,
  "Help",                        34,
  "Hurt",                        53,
  "Neither help nor hurt",       12
)

## --- build panels ---

p_short <- poll_panel(short_term,
                      "Short term")
p_long  <- poll_panel(long_term,
                      "Long term")

## --- combine with one common title & caption ---

common_title1 <- "How do you think Trump's policies on tariffs will affect...\nthe United States economy in the short/long term?"
common_caption1 <- paste(
  "Source: CNN Poll, April 2025, via the Roper Center at Cornell University.",
  "Interview dates: Apr 17–24, 2025 • N = 1678 • Geographic coverage: United States • Survey organization: SSRS • Sponsor: CNN",
  sep = "\n"
)

combined1 <- (p_short / p_long) +
  plot_annotation(
    title   = common_title1,
    caption = common_caption1,
    theme = theme(
      plot.title = element_text(face = "bold", size = 13, margin = margin(b = 6)),
      plot.caption = element_text(size = 8, colour = "grey30", margin = margin(t = 8))
    )
  )

## view
combined1

## save
ggsave("short_vs_long.png", combined1,
       width = 7.5, height = 8.5, dpi = 300)

